package com.ebupt.migu.fineoperationactivitymonitor.common.util;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.AES;

/**
 * Created by Intellij IDEA. User: liuyangyang@cd.ebupt.com Date: 2019/10/23
 */
public class JsonUtils {

    /**
     * 对象转JSON字符串
     *
     * @param object
     * @return
     */
    public static String obj2Json(Object object) {
        return JSON.toJSONString(object);
    }

    /**
     * JSON字符串转对象
     *
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T json2Obj(String str, Class<T> clazz) {
        return JSON.parseObject(str, clazz);
    }

    /**
     * json字符串转含泛型对象
     *
     * @param str
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T json2Obj(String str, TypeReference<T> type) {
        return JSON.parseObject(str, type);
    }

    /**
     * JSON字符串转对象List
     *
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> json2Array(String str, Class<T> clazz) {
        return JSON.parseArray(str, clazz);
    }

    public static void main(String[] args) {
        String url =
            "jdbc:p6spy:mysql://10.25.245.40:3306/rfment_db_label?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai";
        String username = "rfment";
        String password = "Migu@2019";
        String salt = AES.generateRandomKey();
        System.out.println("salt: " + salt);
        System.out.println("URL: " + AES.encrypt(url, salt));
        System.out.println("username: " + AES.encrypt(username, salt));
        System.out.println("password: " + AES.encrypt(password, salt));
    }
}
